package com.four.peoplemanage.gopolice.dao;

import com.four.peoplemanage.gopolice.entity.Gopolice;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface GopoliceDao {
//申请出警
@Insert("insert into gopolice (information_id,applicants,dispatch_date,dispatch_time,department,dispatch_content,dispatch_place,status)"+
        "values (#{information_id},#{applicants},#{dispatch_date},#{dispatch_time},#{department},#{dispatch_content},#{dispatch_place},'待审核')")
public int addGoplice(Gopolice gopolice);
    //分页查询
    @Select("<script>" +
            "SELECT * FROM gopolice" +
            "  <where>" +
            "    <if test=\" applicants != null\">" +
            "       applicants = #{applicants} " +
            "    </if>" +
            "    <if test=\" department != null\">" +
            "       and department = #{department} " +
            "    </if>" +
            "    <if test=\" status != null\">" +
            "       and status = #{status} " +
            "    </if>" +
            "  </where>" +
            "  ORDER BY CASE status WHEN '待审核' THEN 1 ELSE 2 END, dispatch_time ASC " +
            "limit #{start}, #{size}" +
            "</script>")
    public List<Gopolice> getGopolice(int start, int size, String applicants, String department,
                                      String status);
    //分页查询
    @Select("<script>" +
            "SELECT * FROM gopolice" +
            "  <where>" +
            "    <if test=\" applicants != null\">" +
            "       applicants = #{applicants} " +
            "    </if>" +
            "    <if test=\" department != null\">" +
            "       and department = #{department} " +
            "    </if>" +
            "    <if test=\" status != null\">" +
            "       and status = #{status} " +
            "    </if>" +
            "       and information_id=#{information_id}" +
            "  </where>" +
            "  ORDER BY CASE status WHEN '待审核' THEN 1 ELSE 2 END, dispatch_time ASC " +
            "limit #{start}, #{size}" +
            "</script>")
    public List<Gopolice> getOneGopolice(int start, int size, String applicants, String department,int information_id,
                                      String status);

    @Select("<script>" +
            "select count(*) from gopolice" +
            "  <where>" +
            "    <if test=\" applicants != null\">" +
            "       applicants = #{applicants} " +
            "    </if>" +
            "    <if test=\" department != null\">" +
            "       and department = #{department} " +
            "    </if>" +
            "    <if test=\" status != null\">" +
            "       and status = #{status} " +
            "    </if>" +
            "</where>"+
            "</script>")
    public  int countGopolice(String applicants, String department,
                              String status);
    //更新
    @Update("<script>" +
            "update  gopolice " +
            "        <set>" +
            "            <if test=\"null!= status\">" +
            "                status =#{status}," +
            "            </if>" +
            "            <if test=\"null!= dispatch_content\">" +
            "                dispatch_content =#{dispatch_content}," +
            "            </if>" +
            "        </set>" +
            "        where id =#{id}" +
            "</script>")
    public int updateGopoliceById(Gopolice gopolice);

    @Update("<script>" +
            "update  gopolice " +
            "        <set>" +
            "            <if test=\"null!= status\">" +
            "                status =#{status}," +
            "            </if>" +
            "        </set>" +
            "        where id =#{id}" +
            "</script>")
    public int updateGopolice(Gopolice gopolice);

}
